const postDao = require('../dao/postDao');
const fileDao = require('../dao/fileDao');

const postService = {
    async getPostType() {
        let result;
        try {
            result = await postDao.getPostType();
            result = { code: 200, msg: '获取帖子类型成功', data: result };
        } catch (err) {
            result = { code: 500, msg: '服务器错误', data: null };
        }
        return result;
    },
    async addPost(uid, title, content, buffer, type) {
        let result;
        try {
            result = await postDao.addPost(uid, title, content, buffer, type);
            if (result.insertId) {
                result = { code: 200, msg: '发布帖子成功', data: { id: result.insertId } };
            } else {
                result = { code: 500, msg: '发布帖子失败', data: null };
            }
        } catch (err) {
            result = { code: 500, msg: '服务器错误', data: null };
        }
        return result;
    },
    async addComment(uid, postId, content) {
        let result;
        try {
            result = await postDao.addComment(uid, postId, content);
            if (result.insertId) {
                result = { code: 200, msg: '评论成功', data: null };
            } else {
                result = { code: 500, msg: '评论失败', data: null };
            }
        } catch (err) {
            result = { code: 500, msg: '服务器错误', data: null };
        }
        return result;
    },
    async getPostList(uid, cursor, limit) {
        let result;
        let nextCursor = null;
        try {
            result = await postDao.getPostList(uid, cursor, limit);
            if (result.length > limit) {
                nextCursor = result[result.length - 1].id;
                result = result.slice(0, limit);
            }
            for (let i = 0; i < result.length; i++) {
                // 获取用户头像
                let tmp = await fileDao.getAvatarUrl(result[i].uid);
                if (tmp.success) {
                    result[i].avatar = tmp.url;
                } else {
                    result[i].avatar = '';
                }
                // 获取树洞图片
                tmp = await fileDao.getTreeHoleImgUrl(result[i].imgId);
                if (tmp.success) {
                    result[i].imageUrl = tmp.url;
                } else {
                    result[i].imageUrl = '';
                }
            }
            result = { code: 200, msg: '获取帖子列表成功', data: { items: result, nextCursor } };
        } catch (err) {
            result = { code: 500, msg: '服务器错误', data: null };
        }
        return result;
    },
    async getPostLikeByType(uid, typeId, cursor, limit) {
        let result;
        let nextCursor = null;
        try {
            result = await postDao.getPostLikeByType(uid, typeId, cursor, limit);
            if (result.length > limit) {
                nextCursor = result[result.length - 1].id;
                result = result.slice(0, limit);
            }
            for (let i = 0; i < result.length; i++) {
                // 获取用户头像
                let tmp = await fileDao.getAvatarUrl(result[i].uid);
                if (tmp.success) {
                    result[i].avatar = tmp.url;
                } else {
                    result[i].avatar = '';
                }
                // 获取树洞图片
                tmp = await fileDao.getTreeHoleImgUrl(result[i].imgId);
                if (tmp.success) {
                    result[i].imageUrl = tmp.url;
                } else {
                    result[i].imageUrl = '';
                }
            }
            result = { code: 200, msg: '获取帖子列表成功', data: { items: result, nextCursor } };
        } catch (err) {
            result = { code: 500, msg: '服务器错误', data: null };
        }
        return result;
    },
    async getPostComment(postId, cursor, limit) {
        let result;
        let nextCursor = null;
        try {
            result = await postDao.getPostComment(postId, cursor, limit);
            if (result.length > limit) {
                nextCursor = result[result.length - 1].id;
                result = result.slice(0, limit);
            }
            for (let i = 0; i < result.length; i++) {
                // 获取用户头像
                let tmp = await fileDao.getAvatarUrl(result[i].uid);
                if (tmp.success) {
                    result[i].avatar = tmp.url;
                } else {
                    result[i].avatar = '';
                }
            }
            result = { code: 200, msg: '获取帖子评论成功', data: { items: result, nextCursor } };
        } catch (err) {
            result = { code: 500, msg: '服务器错误', data: null };
        }
        return result;
    },
    async like(uid, postId, is_like) {
        let result;
        try {
            result = await postDao.like(uid, postId, is_like);
            result = { code: 200, msg: is_like ? '点赞成功' : '取消点赞成功', data: null };
        } catch (err) {
            result = { code: 500, msg: '服务器错误', data: null };
        }
        return result;
    },
    async collect(uid, postId, is_collect) {
        let result;
        try {
            result = await postDao.collect(uid, postId, is_collect);
            result = { code: 200, msg: is_collect ? '收藏成功' : '取消收藏成功', data: null };
        } catch (err) {
            result = { code: 500, msg: '服务器错误', data: null };
        }
        return result;
    },
    async getCollectPostlist(uid, cursor, limit) {
        let result;
        let nextCursor = null;
        try {
            result = await postDao.getCollectPostlist(uid, cursor, limit);
            if (result.length > limit) {
                nextCursor = result[result.length - 1].collectId;
                result = result.slice(0, limit);
            }
            for (let i = 0; i < result.length; i++) {
                // 获取用户头像
                let tmp = await fileDao.getAvatarUrl(result[i].uid);
                if (tmp.success) {
                    result[i].avatar = tmp.url;
                } else {
                    result[i].avatar = '';
                }
                // 获取树洞图片
                tmp = await fileDao.getTreeHoleImgUrl(result[i].imgId);
                if (tmp.success) {
                    result[i].imageUrl = tmp.url;
                } else {
                    result[i].imageUrl = '';
                }
            }
            result = { code: 200, msg: '获取帖子收藏列表成功', data: { items: result, nextCursor } };
        } catch (err) {
            result = { code: 500, msg: '服务器错误', data: null };
        }
        return result;
    },
    async getPostDetail(uid, postId) {
        let result;
        try {
            result = await postDao.getPostDetail(uid, postId);
            if (result.length > 0) {
                result = result[0];
                // 获取用户头像
                let tmp = await fileDao.getAvatarUrl(result.uid);
                if (tmp.success) {
                    result.avatar = tmp.url;
                } else {
                    result.avatar = '';
                }
                // 获取树洞图片
                tmp = await fileDao.getTreeHoleImgUrl(result.imgId);
                if (tmp.success) {
                    result.imageUrl = tmp.url;
                } else {
                    result.imageUrl = '';
                }
                result = { code: 200, msg: '获取帖子成功', data: result };
            } else {
                result = { code: 404, msg: '帖子不存在', data: null };
            }
        } catch (err) {
            result = { code: 500, msg: '服务器错误', data: null };
        }
        return result;
    },
    async getUnreadCount(uid) {
        let result;
        try {
            result = await postDao.getUnreadCount(uid);
            result = { code: 200, msg: '获取未读帖子数成功', data: result[0] };
        } catch (err) {
            result = { code: 500, msg: '服务器错误', data: null };
        }
        return result;
    },
    async likeRead(uid) {
        let result;
        try {
            result = await postDao.likeRead(uid);
            result = { code: 200, msg: '标记成功', data: null };
        } catch (err) {
            result = { code: 500, msg: '服务器错误', data: null };
        }
        return result;
    },
    async collectRead(uid) {
        let result;
        try {
            result = await postDao.collectRead(uid);
            result = { code: 200, msg: '标记成功', data: null };
        } catch (err) {
            result = { code: 500, msg: '服务器错误', data: null };
        }
        return result;
    },
    async commentRead(uid) {
        let result;
        try {
            result = await postDao.commentRead(uid);
            result = { code: 200, msg: '标记成功', data: null };
        } catch (err) {
            result = { code: 500, msg: '服务器错误', data: null };
        }
        return result;
    },
    async getLikeListLazyLoad(uid, cursor, limit) {
        let result;
        let nextCursor = null;
        try {
            result = await postDao.getLikeListLazyLoad(uid, cursor, limit);
            if (result.length > limit) {
                nextCursor = result[result.length - 1].id;
                result = result.slice(0, limit);
            }
            for (let i = 0; i < result.length; i++) {
                // 获取用户头像
                let tmp = await fileDao.getAvatarUrl(result[i].uid);
                if (tmp.success) {
                    result[i].avatar = tmp.url;
                } else {
                    result[i].avatar = '';
                }
                // 获取树洞图片
                tmp = await fileDao.getTreeHoleImgUrl(result[i].imgId);
                if (tmp.success) {
                    result[i].imageUrl = tmp.url;
                } else {
                    result[i].imageUrl = '';
                }
            }
            result = { code: 200, msg: '获取成功', data: { items: result, nextCursor } };
        } catch (err) {
            result = { code: 500, msg: '服务器错误', data: null };
        }
        return result;
    },
    async getCollectListLazyLoad(uid, cursor, limit) {
        let result;
        let nextCursor = null;
        try {
            result = await postDao.getCollectListLazyLoad(uid, cursor, limit);
            if (result.length > limit) {
                nextCursor = result[result.length - 1].id;
                result = result.slice(0, limit);
            }
            for (let i = 0; i < result.length; i++) {
                // 获取用户头像
                let tmp = await fileDao.getAvatarUrl(result[i].uid);
                if (tmp.success) {
                    result[i].avatar = tmp.url;
                } else {
                    result[i].avatar = '';
                }
                // 获取树洞图片
                tmp = await fileDao.getTreeHoleImgUrl(result[i].imgId);
                if (tmp.success) {
                    result[i].imageUrl = tmp.url;
                } else {
                    result[i].imageUrl = '';
                }
            }
            result = { code: 200, msg: '获取成功', data: { items: result, nextCursor } };
        } catch (err) {
            result = { code: 500, msg: '服务器错误', data: null };
        }
        return result;
    },
    async getCommentListLazyLoad(uid, cursor, limit) {
        let result;
        let nextCursor = null;
        try {
            result = await postDao.getCommentListLazyLoad(uid, cursor, limit);
            if (result.length > limit) {
                nextCursor = result[result.length - 1].id;
                result = result.slice(0, limit);
            }
            for (let i = 0; i < result.length; i++) {
                // 获取用户头像
                let tmp = await fileDao.getAvatarUrl(result[i].uid);
                if (tmp.success) {
                    result[i].avatar = tmp.url;
                } else {
                    result[i].avatar = '';
                }
                // 获取树洞图片
                tmp = await fileDao.getTreeHoleImgUrl(result[i].imgId);
                if (tmp.success) {
                    result[i].imageUrl = tmp.url;
                } else {
                    result[i].imageUrl = '';
                }
            }
            result = { code: 200, msg: '获取成功', data: { items: result, nextCursor } };
        } catch (err) {
            result = { code: 500, msg: '服务器错误', data: null };
        }
        return result;
    }
};

module.exports = postService;